package com.example.mynote.ui.clock;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

import com.example.mynote.R;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

public class EditAlarmClock extends AppCompatActivity {
    //    Button ret;
//    Button save;
    TitleBar titleBar;
    //    EditText hourTextView;
//    EditText minuteTextView;
    EditText remarkTextView;
    TimePicker picker;
    String type; //操作类型 edit：修改   new：新增
    String TAG = "EditAlarmClock";

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //隐藏标题栏，无效
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_edit_alarm_clock);

        titleBar = findViewById(R.id.bar);
//        ret = findViewById(R.id.bar_id_left_view);
//        save = (Button)findViewById(R.id.bar_id_right_view);
//        hourTextView = (EditText)findViewById(R.id.hour);
//        minuteTextView = (EditText)findViewById(R.id.minute);
        remarkTextView = (EditText)findViewById(R.id.remark);
        picker = (TimePicker)findViewById(R.id.picker);
        picker.setIs24HourView(true); //24小时显示

        //得到启动此活动的活动
        Intent firstIntent = getIntent();
        //TODO:怎样复用？
//        Integer f = firstIntent.getFlags()

        //得到传入的数据
        Bundle b = firstIntent.getExtras();
        type = b.getString("type");
        Log.d("活动2bubble传入类型", type);
        //如果是编辑闹钟，从bubble里取出闹钟之前的信息为EditText视图设置预设字符串
        if(type.equals("edit"))
        {
            String hour_passed=b.getString("hour_passed");
            String minute_passed=b.getString("minute_passed");
            String remark_passed=b.getString("remark_passed");
//            hourTextView.setText(hour_passed);
//            minuteTextView.setText(minute_passed);
            remarkTextView.setText(remark_passed);

            Integer old_hour = Integer.parseInt(hour_passed);
            Integer old_minute = Integer.parseInt(minute_passed);
            picker.setHour(old_hour);
            picker.setMinute(old_minute);
        }
//        save.setEnabled(false);
//
//        ret.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                onBackPressed();
//            }
//        });
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                EditAlarmClock.this.finish();
            }
            @Override
            public void onTitleClick(View v) {

            }
            @Override
            public void onRightClick(View v) {
                //实现保存
                save();
            }
        });

    }

    private void save()
    {
        String hour=String.valueOf(picker.getCurrentHour());
        String minute=String.valueOf(picker.getCurrentMinute());

        String remark=remarkTextView.getText().toString().trim();

        if(TextUtils.isEmpty(remark)){
            remark="";
        }

        Intent intent = new Intent();
//        intent.putExtra("hour_return", name);
//        intent.putExtra("minute_return", number);
//        intent.putExtra("remark_return", remark);
        //使用bubble作为容器，相应使用一个参数的putExtras而非putExtra
        Bundle bundle=new Bundle();
        bundle.putString("type", type); //传回操作类型
        bundle.putString("hour_return", hour);
        bundle.putString("minute_return", minute);
        bundle.putString("remark_return", remark);
        intent.putExtras(bundle);

        setResult(RESULT_OK,intent);
        finish();             // 实现退出
    }
}